<template>
  <u-modal :show="showModal" title="选择-机台" @cancel="onClose" @confirm="onConfirm" :showCancelButton="true">
    <view class="slot-content width-100-percentage">
      <jk-table
          :height="400"
          :stripe="false"
          :table-header="tableHeader"
          :table-data="tableData"
          @checked="onCheck"
      ></jk-table>
    </view>
  </u-modal>
</template>

<script>
  import {tipBar, showLoading} from "../../../../utils/jk-utils";
  import {prdNoticeMachineListWithChangeProduct} from "../../../../api";
  import jkPage from '../../../../jk-components/jk-page';
  import jkTable from '../../../../jk-components/jk-table';

  export default {
    components: { jkPage, jkTable },
    props: {
      show: {
        type: Boolean,
        default: false,
      },
      noticeOrderId: {
        type: Number,
        default: null,
      }
    },
    data() {
      return {
        tableHeader: [
          { type:'selection', width:50 },
          { title: '机台', key: 'machineCode' },
          { title: '开台状态', key: 'openingStateName' },
          { title: '预计开台时间', key: 'subPlanStartDate' },
          { title: '实际开台时间', key: 'actualStartDate' },
          { title: '排产天数', key: 'productionDays' },
          { title: '排产数量', key: 'productionQty' },
          { title: '剩余天数', key: 'surplusDays', align: 'right' },
          { title: '未完数量', key: 'unfinishedQty', align: 'right' },
          { title: '改前品种', key: 'preProductName', align: 'left' },
          { title: '改前排产数量', key: 'preProductionQty', align: 'right' },
          { title: '改前未完数量', key: 'preUnfinishedQty', align: 'right' },
        ],
        checkArr: [],
        tableData: [],
        showModal: false
      };
    },
    methods: {
      onCheck(e) {
        console.log('值', e)
        this.checkArr = e;
      },
      onSearchBtn() {
        this.checkArr = [];
        this.getListRequest();
      },
      getListRequest() {
        if (!this.noticeOrderId) return;
        showLoading(true);
        return prdNoticeMachineListWithChangeProduct([this.noticeOrderId]).then(res => {
          showLoading(false);
          if (res.data.status === 200) {
            this.tableData = res.data.res.map(x => {
              if (x.openingState === 0) {
                x.openingStateName = '未开台';
                x.disabled = true;
              } else if (x.openingState === 1) {
                x.openingStateName = '开台中';
              } else if (x.openingState === 2) {
                x.openingStateName = '已了机';
              }
              return x;
            });
          }
        });
      },
      onClose() {
        this.$emit('close');
        this.tableData = [];
      },
      onConfirm() {
        console.log('值1111', this.checkArr)
        if (this.checkArr.length) {
          console.log('值1111', this.checkArr)
          this.$emit('confirm', JSON.parse(JSON.stringify(this.checkArr)));
          this.tableData = [];
        } else {
          tipBar('none');
        }
      },
    },
    watch: {
      show(newVal) {
        this.showModal = newVal;
        if (newVal) {
          this.getListRequest();
        }
      }
    }
  }
</script>
